package com.sifeng.test;

import com.sifeng.pojo.WorkloadRatioDTO;
import com.sifeng.utils.MyUtils;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang_gsh on 2018/9/11.
 * 创建复评材料
 */
public class CreateReappraisalMaterials {
	private static String date = "201908";    //修改成当月
	private static String basePath = "E:\\hisun\\开发三部\\功能点评估表\\";
	private static String path = basePath + date  + "\\";
	private static String reappraisalMaterialsPath = path + "8月开发复评材料-杨贵生\\";  //修改月份
	private static String listFilePath = path + "需求列表_201908.xlsx";

	private static int finishThisMonthNum = 0;

	//private static String LastDate = "201809";  //修改成上个月
	//private static String fileName = "需求列表_" + date + ".xlsx";
	//private static String lastPath = "E:\\hisun\\开发三部\\功能点评估表\\" + LastDate  + "\\";


	public static void copyFile(String sourcePath, String dstPath) {
		try {
			FileUtils.copyFile(new File(sourcePath), new File(dstPath));
		} catch (IOException e) {
			System.out.println(sourcePath + "not found : " + e.getMessage());
		}

	}

	public static List<WorkloadRatioDTO> getDemandList(String path) throws IOException {
		XSSFWorkbook wb = MyUtils.getXSSFWorkbook(path);
		XSSFSheet sheet = wb.getSheetAt(0);
		List<WorkloadRatioDTO> list = new ArrayList<WorkloadRatioDTO>();
		if (sheet == null) {
			return null;
		}

		for(int i = 1; i < sheet.getLastRowNum(); i++) {
			XSSFRow row = sheet.getRow(i);
			XSSFCell cell_A = row.getCell(0);
			if(cell_A == null) {
				continue;
			}
			//cell_A.setCellType(Cell.CELL_TYPE_STRING);
			String requirementNumber = cell_A.getStringCellValue();

			if("".equals(requirementNumber.trim())) {
				continue;
			}

			XSSFCell cell_B = row.getCell(1);
			XSSFCell cell_I = row.getCell(8);

			WorkloadRatioDTO workloadRatioDTO = new WorkloadRatioDTO();
			workloadRatioDTO.setRequirementNumber(requirementNumber.trim());
			workloadRatioDTO.setRequirementName(cell_B.getStringCellValue());
			workloadRatioDTO.setRemainingWork(cell_I.getNumericCellValue());
			workloadRatioDTO.setMonth(date);
			list.add(workloadRatioDTO);
		}

		XSSFSheet sheet3 = wb.getSheetAt(2);
		if (sheet3 == null) {
			return list;
		}

		for(int i = 1; i < sheet3.getLastRowNum(); i++) {
			XSSFRow row = sheet3.getRow(i);
			XSSFCell cell_A = row.getCell(0);
			if(cell_A == null) {
				continue;
			}
			String requirementNumber = cell_A.getStringCellValue();

			if("".equals(requirementNumber.trim())) {
				continue;
			}

			XSSFCell cell_B = row.getCell(1);
			XSSFCell cell_L = row.getCell(11);
			XSSFCell cell_M = row.getCell(12);
			cell_M.setCellType(Cell.CELL_TYPE_STRING);
			WorkloadRatioDTO workloadRatioDTO = new WorkloadRatioDTO();
			workloadRatioDTO.setRequirementNumber(requirementNumber.trim());
			workloadRatioDTO.setRequirementName(cell_B.getStringCellValue());
			workloadRatioDTO.setRemainingWork(cell_L.getNumericCellValue());
			workloadRatioDTO.setMonth(cell_M.getStringCellValue());
			list.add(workloadRatioDTO);
		}

		return list;
	}

	public static void moveFile(WorkloadRatioDTO workloadRatioDTO, String resourcePath) {
		String cellReqNameValue = workloadRatioDTO.getRequirementName();
		String cellReqNumberValue = workloadRatioDTO.getRequirementNumber();
		String preFileNameTmp =  cellReqNumberValue + "_" + cellReqNameValue;
		File reappraisalMaterialsReqFile = new File(reappraisalMaterialsPath + preFileNameTmp);
		if(!reappraisalMaterialsReqFile.exists()){//如果文件夹不存在
			reappraisalMaterialsReqFile.mkdir();//创建文件夹
		}

		List<String> technicalProposalSpecificationList = MyUtils.searchFile(resourcePath, cellReqNumberValue, "_技术方案说明书", ".docx");
		for (String s : technicalProposalSpecificationList) {
			CreateReappraisalMaterials.copyFile(resourcePath + s,reappraisalMaterialsPath + preFileNameTmp + "\\" + s);
		}

		List<String> atomicFunctionPointList = MyUtils.searchFile(resourcePath, cellReqNumberValue, "_原子功能点评估表(电子工单)", ".xlsx");
		for (String s : atomicFunctionPointList) {
			CreateReappraisalMaterials.copyFile(resourcePath + s, reappraisalMaterialsPath + preFileNameTmp + "\\" + s);
		}

		List<String>  requirementsSpecificationList = MyUtils.searchFile(resourcePath, cellReqNumberValue, "需求方案说明书", null);
		for (String s : requirementsSpecificationList) {
			CreateReappraisalMaterials.copyFile(resourcePath + s, reappraisalMaterialsPath + preFileNameTmp + "\\" + s);
		}

		List<String>  requirementsSpecificationList1 = MyUtils.searchFile(resourcePath, cellReqNumberValue, "需求说明书", null);
		for (String s : requirementsSpecificationList1) {
			CreateReappraisalMaterials.copyFile(resourcePath + s, reappraisalMaterialsPath + preFileNameTmp + "\\" + s);
		}

		printNotFound(preFileNameTmp, technicalProposalSpecificationList.size(), atomicFunctionPointList.size(),
				requirementsSpecificationList.size() + requirementsSpecificationList1.size());
		finishThisMonthNum++;
	}

	public static void fetchList(List<WorkloadRatioDTO> list) {
		for (WorkloadRatioDTO workloadRatioDTO : list) {
			if(workloadRatioDTO.getRemainingWork() == 0) {
				String resourcePath = basePath + workloadRatioDTO.getMonth() + "\\";
				moveFile(workloadRatioDTO, resourcePath);
			} else {
				System.out.println(workloadRatioDTO.getRequirementNumber() + "_" + workloadRatioDTO.getRequirementName() + "：本月未完成");
			}
		}
	}

	public static void printNotFound(String requirement, int technicalProposalSpecificationListSize, int atomicFunctionPointListSize, int requirementsSpecificationListSize) {
		String printStr = requirement + "：";
		if(technicalProposalSpecificationListSize == 0 || atomicFunctionPointListSize == 0 || requirementsSpecificationListSize == 0) {
			if(technicalProposalSpecificationListSize == 0) {
				printStr += "-技术方案未找到";
			}

			if(atomicFunctionPointListSize == 0) {
				printStr += "-原子功能点未找到";
			}

			if(requirementsSpecificationListSize == 0) {
				printStr += "-需求说明书未找到";
			}
		} else {
			printStr += "已完成复制";
		}

		System.out.println(printStr);
	}

	/*public static void DemandForTheMonth(XSSFSheet sheet, String resourcePath) {
		boolean whileFlg = true;
		int index = 0;
		while (whileFlg) {
			index++;
			XSSFRow row = sheet.getRow(index);
			XSSFCell cellReqNumber = row.getCell(0);
			String cellReqNumberValue = cellReqNumber.getStringCellValue();
			if (cellReqNumberValue == null || "".equals(cellReqNumberValue.trim())) {
				break;
			}

			XSSFCell cellReqRemainNumber = row.getCell(8);
			cellReqRemainNumber.setCellType(Cell.CELL_TYPE_STRING);
			String cellReqRemainNumberValue = cellReqRemainNumber.getStringCellValue();

			if (cellReqRemainNumberValue == null || "".equals(cellReqRemainNumberValue.trim())) {
				break;
			}

			if (Integer.parseInt(cellReqRemainNumberValue.trim()) != 0) {
				continue;
			}

			XSSFCell cellReqName = row.getCell(1);
			String cellReqNameValue = cellReqName.getStringCellValue();
			String preFileNameTmp =  cellReqNumberValue.trim() + "_" + cellReqNameValue.trim();
			File reappraisalMaterialsReqFile = new File(reappraisalMaterialsPath + preFileNameTmp);
			if(!reappraisalMaterialsReqFile.exists()){//如果文件夹不存在
				reappraisalMaterialsReqFile.mkdir();//创建文件夹
			}

			List<String> TechnicalProposalSpecificationList = MyUtils.searchFile(resourcePath, preFileNameTmp + "_技术方案说明书", ".docx");
			for (String s : TechnicalProposalSpecificationList) {
				CreateReappraisalMaterials.copyFile(resourcePath + s,reappraisalMaterialsPath + preFileNameTmp + "\\" + s);
			}

			List<String> AtomicFunctionPointList = MyUtils.searchFile(resourcePath, preFileNameTmp + "_原子功能点评估表_", ".xlsx");
			for (String s : AtomicFunctionPointList) {
				CreateReappraisalMaterials.copyFile(resourcePath + s, reappraisalMaterialsPath + preFileNameTmp + "\\" + s);
			}

			List<String>  requirementsSpecificationList = MyUtils.searchFile(resourcePath, cellReqNumberValue + "_", "需求方案说明书", null);
			for (String s : requirementsSpecificationList) {
				System.out.println(s);
				CreateReappraisalMaterials.copyFile(resourcePath + s, reappraisalMaterialsPath + preFileNameTmp + "\\" + s);
			}

		}
	}*/
	public static void main(String[] args) throws Exception {
		File reappraisalMaterialsfile = new File(reappraisalMaterialsPath);
		if(!reappraisalMaterialsfile.exists()){//如果文件夹不存在
			reappraisalMaterialsfile.mkdir();//创建文件夹
		}

		List<WorkloadRatioDTO> list = CreateReappraisalMaterials.getDemandList(listFilePath);

		CreateReappraisalMaterials.fetchList(list);

		System.out.println("总需求数：" + list.size());
		System.out.println("本月完成需求数：" + finishThisMonthNum);


		/*InputStream iStream=new FileInputStream(path + fileName);
		XSSFWorkbook wb = new XSSFWorkbook(iStream);
		XSSFSheet sheet = wb.getSheetAt(0);
		if (sheet == null) {
			return;
		}
		System.out.println("finish....1");

		System.out.println("begin....2");
		CreateReappraisalMaterials.DemandForTheMonth(sheet, path);

		XSSFSheet sheet2 = wb.getSheetAt(2);
		if (sheet2 == null) {
			return;
		}

		CreateReappraisalMaterials.DemandForTheMonth(sheet2, lastPath);

		iStream.close();

		System.out.println("finish....2");*/

	}
}
